#include "WMarkConfig.h"
#include "log/log.h"
namespace android
{
    WMarkConfig *WMarkConfig::mSingleton = NULL;

    // 默认配置
    int WMarkConfig::mFontSzie = 24;              // 字体大小
    int WMarkConfig::mWatermarkImageHeight = 24;  // 生成水印图片的 高， 尽量和字体大小保持一致即可
    int WMarkConfig::mWatermarkImageWidth = 1028; // 生成水印图片的 宽,  尽量和分辨率的宽保持一致即可
    int WMarkConfig::mWmarkDisplayPosition_x = 0; // 水印在 display 时，显示的位置
    int WMarkConfig::mWmarkDisplayPosition_y = 24;
    bool WMarkConfig::mEnableWatermark = true; // 指定是否启用水印功能
    string WMarkConfig::mFontLibPath = "/vendor/etc/Roboto-BoldItalic.ttf"; // 指定字体库的路径
    string WMarkConfig::mConfigJsonPath = "/vendor/etc/WaterMarkConfig.json";

    WMarkConfig *WMarkConfig::getInstance()
    {
        if (mSingleton == NULL)
        {
            mSingleton = new WMarkConfig();
        }
        return mSingleton;
    }

    WMarkConfig::WMarkConfig()
    {
        Json::Value root;
        Json::Reader reader;
        std::ifstream ifs(mConfigJsonPath); //open file example.json

        if (!reader.parse(ifs, root))
        {
            // fail to parse
            ALOGE("fail to parse: %s\n", mConfigJsonPath.c_str());
        }
        else
        {
            mFontSzie = root["font-szie"].asInt();
            mWatermarkImageHeight= root["watermark-image-height"].asInt();
            mWatermarkImageWidth= root["watermark-image-width"].asInt();
            mWmarkDisplayPosition_x= root["display_x"].asInt();
            mWmarkDisplayPosition_y= root["display_y"].asInt();
            mEnableWatermark = root["enable-watermark"].asBool();
            mFontLibPath = root["font-lib-path"].asString();
        }
        printConfig();
    }

    WMarkConfig::~WMarkConfig()
    {
    }

    void WMarkConfig::printConfig() {
        ALOGD("++++++++++++++++++++++++++++++++");
        ALOGD("- enable : %s", mEnableWatermark ? "on": "off");
        ALOGD("- font-size : %d", mFontSzie);
        ALOGD("- watermark-image : %dx%d", mWatermarkImageHeight, mWatermarkImageWidth);
        ALOGD("- display-position : (%d,%d)", mWmarkDisplayPosition_x, mWmarkDisplayPosition_y);
        ALOGD("- font-path : %s", mFontLibPath.c_str());
        ALOGD(" ++++++++++++++++++++++++++++++++");
    }
}